package com.android.powerbankpad.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.android.powerbankpad.ui.MyFragment;

import java.util.List;

public class AdAdapter extends FragmentStatePagerAdapter {

    private List<MyFragment> dataList;

    public AdAdapter(@NonNull FragmentManager fm) {
        super(fm);
    }


    public List<MyFragment> getDataList() {
        return dataList;
    }

    public void setDataList(List<MyFragment> dataList) {
        if (null == dataList) {
            notifyDataSetChanged();
            return;
        }
        this.dataList = dataList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public Fragment getItem(int position) {
       /* if (dataList.size() > 1) {
            return dataList.get(position % dataList.size());
        } else {
            return dataList.get(position);
        }*/
        return dataList.get(position);
    }


    @Override
    public int getCount() {
        if (dataList != null) {
           /* if (dataList.size() > 3) {
                return Integer.MAX_VALUE; //无限循环 size需大于=3 不然会报错
            } else {
                return dataList.size();
            }*/
            return dataList.size();
        } else {
            return 0;
        }
    }


    public void removeFragment() {
        for (int i = 0; i < dataList.size(); i++) {
            dataList.get(i).onDestroy();
        }
        if (dataList != null) {
            dataList.clear();
        }
        notifyDataSetChanged();
    }
}
